<?php
class commentclass {

	function commentclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}

	function add_comment($page_id, $parent_id, $parent_type, $parent_name, $data){
    $host = $_SERVER['REMOTE_ADDR'];
		if (is_array($data) && (count($data) > 0)){
			$parent_type = trim($parent_type); $parent_type = addslashes($parent_type);
			$parent_name = trim($parent_name); $parent_name = addslashes($parent_name);
			if ((strlen($parent_type) > 0) && (strlen($parent_type) > 0)){
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."comment` (`id`,`page_id`,`parent_id`,`parent_type`,`parent_name`,`date`,`ip`) VALUES ('', ".$page_id.", ".$parent_id.", '".$parent_type."', '".$parent_name."', '".date("YmdHis")."', '".$host."')", __LINE__, __FILE__);
				$parent = mysql_insert_id();
				foreach ($data as $key=>$value){
					if (isset($value['name']) && isset($value['type']) && isset($value['value']) && isset($value['required'])){
						$value['name'] = trim($value['name']); $value['name'] = addslashes($value['name']);
						$value['type'] = trim($value['type']); $value['type'] = addslashes($value['type']);
						$value['value'] = trim($value['value']); $value['value'] = addslashes($value['value']);
						$value['required'] = intval($value['required']);
						if (($value['required'] == 0) || (($value['required'] == 1) && (strlen($value['value']) > 0)))
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."comment_data` (`id`,`parent`,`key`,`name`,`type`,`value`,`required`) VALUES ('', ".$parent.", '".$key."', '".$value['name']."', '".$value['type']."', '".$value['value']."', ".$value['required'].")", __LINE__, __FILE__);
						else return 23;
					} else return 15;
				}
				return 0;
			} else return 15;
		} else return 15;
	}
	
	function get_comments($parent_id, $parent_type){
		$result = array();
		$parent_type = trim($parent_type); $parent_type = addslashes($parent_type);
		$sql = $this->mysqlclass->mysql_my_query("
			SELECT `t1`.`date` `parent_date`, `t2`.* FROM `".$this->setupdata['table_prefix']."comment` `t1`, `".$this->setupdata['table_prefix']."comment_data` `t2`
			WHERE `t1`.`parent_id` = ".$parent_id." AND `t1`.`parent_type` = '".$parent_type."' AND `t2`.`parent` = `t1`.`id`
		", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$row['value'] = stripslashes($row['value']);
			$result[$parent_id][$row['parent']]['date'] = $row['parent_date'];
			$result[$parent_id][$row['parent']][$row['name']] = $row['value'];
		}
		return $result;
	}
	
	function get_all_comments(){
		global $treeclass;
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("
			SELECT `t1`.*, `t2`.* FROM `".$this->setupdata['table_prefix']."comments` `t1`, `".$this->setupdata['table_prefix']."comments_data` `t2`
			WHERE `t2`.`parent` = `t1`.`id`
		", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if (!isset($result[$row['page_id']])){
				if ($row['page_id'] > 0) $result[$row['page_id']] = $treeclass->get_item_info($row['page_id']);
				else $result[$row['page_id']] = array();
			}
			$row['name'] = stripslashes($row['name']);
			$row['value'] = stripslashes($row['value']);
			$row['type'] = stripslashes($row['type']);
			$result[$row['page_id']]['comments'][$row['parent']]['date'] = $row['date'];
			$result[$row['page_id']]['comments'][$row['parent']][$row['key']]['name'] = $row['name'];
			$result[$row['page_id']]['comments'][$row['parent']][$row['key']]['type'] = $row['type'];
			$result[$row['page_id']]['comments'][$row['parent']][$row['key']]['value'] = $row['value'];
			$result[$row['page_id']]['comments'][$row['parent']][$row['key']]['required'] = $row['required'];
		}

		return $result;
	}
	
	function del_tree_elements($page_id){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."comment` WHERE ".$page_id."=`page_id`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment_data` WHERE `parent`=".$row['parent_id'], __LINE__, __FILE__);		
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment` WHERE `page_id`=".$page_id, __LINE__, __FILE__);		
		}
	}
	
	function get_all_pages(){
		global $treeclass;
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.* FROM `".$this->setupdata['table_prefix']."comment` `t1`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if (!isset($result[$row['page_id']])){
				if ($row['page_id'] > 0) $tmp = $treeclass->get_item_info($row['page_id']);
				if ($tmp != NULL) $result[$tmp['name']] = $tmp;
			}
		}
		asort($result); reset($result);
		$data = $result;
		$result = array();
		foreach ($data as $key=>$value) $result[$value['id']] = $value;
		return $result;
	}
	
	function get_all_elements_by_page_id($page_id){
		global $treeclass;
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `t1`.* FROM `".$this->setupdata['table_prefix']."comment` `t1` WHERE '".$page_id."'=`page_id`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['parent_name'] = stripslashes($row['parent_name']);
			$row['parent_type'] = stripslashes($row['parent_type']);
			$result[$row['parent_name']]['id'] = $row['parent_id'];
			$result[$row['parent_name']]['type'] = $row['parent_type'];
			$result[$row['parent_name']]['name'] = $row['parent_name'];
		}
		asort($result); reset($result);
		$data = $result;
		$result = array();
		foreach ($data as $key=>$value) $result[$value['id']] = $value;
		return $result;
	}
	
	function get_comments_by_page_id_parent_id_and_parent_type($parent_id, $parent_type){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("
			SELECT `t1`.`date` `parent_date`, `t1`.`parent_id`, `t1`.`parent_type`, `t1`.`parent_name`, `t1`.`date` `parent_date`, `t2`.* FROM `".$this->setupdata['table_prefix']."comment` `t1`, `".$this->setupdata['table_prefix']."comment_data` `t2`
			WHERE `t1`.`parent_id` = ".$parent_id." AND `t1`.`parent_type` = '".$parent_type."' AND `t2`.`parent` = `t1`.`id`
		", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			if (!isset($result['main'])){
				$row['parent_name'] = stripslashes($row['parent_name']);
				$row['parent_type'] = stripslashes($row['parent_type']);
				$result['main']['id'] = $row['parent_id'];
				$result['main']['type'] = $row['parent_type'];
				$result['main']['name'] = $row['parent_name'];
			}
			$row['name'] = stripslashes($row['name']);
			$row['value'] = stripslashes($row['value']);
			$row['type'] = stripslashes($row['type']);
			$result['data'][$row['parent']]['date'] = $row['parent_date'];
			$result['data'][$row['parent']][$row['key']]['name'] = $row['name'];
			$result['data'][$row['parent']][$row['key']]['type'] = $row['type'];
			$result['data'][$row['parent']][$row['key']]['value'] = $row['value'];
			$result['data'][$row['parent']][$row['key']]['required'] = $row['required'];
		}
		return $result;
	}
	
	function delete_comment_by_page($page_id){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."comment` WHERE '".$page_id."'=`page_id`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row['id'];
		foreach ($result as $key=>$value){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment_data` WHERE ".$key."=`parent`", __LINE__, __FILE__);		
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment` WHERE ".$key."=`id`", __LINE__, __FILE__);		
		}
	}

	function delete_comment_by_parent($parent_id, $parent_type){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."comment` WHERE '".$parent_id."'=`parent_id` AND '".$parent_type."'=`parent_type`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row['id'];
		foreach ($result as $key=>$value){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment_data` WHERE ".$key."=`parent`", __LINE__, __FILE__);		
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment` WHERE ".$key."=`id`", __LINE__, __FILE__);		
		}
	}

	function delete_comment_by_comment_id($comment_id){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment_data` WHERE ".$comment_id."=`parent`", __LINE__, __FILE__);		
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment` WHERE ".$comment_id."=`id`", __LINE__, __FILE__);		
	}

	function delete_all_comment(){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment_data`", __LINE__, __FILE__);		
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."comment`", __LINE__, __FILE__);		
	}
}
?>
